package com.yeheng;

/**
 * @Classname Thread2
 * @Description :
 * @Date 2021/2/27 11:54
 * @Created by Yeheng
 */


class Test implements Runnable {
    private int j = 0;

    @Override
    public void run() {
        while (true) {

            synchronized (this) {
                String name = Thread.currentThread().getName();
                if ("线程一".equals(name) || "线程二".equals(name)) {
                    System.out.println(Thread.currentThread().getName() + "j当前为：" + j--);
                } else {
                    System.out.println(Thread.currentThread().getName() + "j当前为：" + j++);
                }
            }
        }

    }
}

public class ThreadTest2 {
    public static void main(String[] args) {
        Test test = new Test();
        Thread thread = new Thread(test, "线程一");
        Thread thread2 = new Thread(test, "线程二");
        Thread thread3 = new Thread(test, "线程三");
        Thread thread4 = new Thread(test, "线程四");
        thread.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
